package com.edu.labmanager.controller;


import com.edu.labmanager.entity.College;
import com.edu.labmanager.entity.LabAdminManagement;
import com.edu.labmanager.entity.Laboratory;
import com.edu.labmanager.entity.User;
import com.edu.labmanager.service.ICollegeService;
import com.edu.labmanager.service.ILabAdminManagementService;
import com.edu.labmanager.service.ILaboratoryService;
import com.edu.labmanager.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 实验室管理员管理表，用于记录实验室管理员与实验室的管理关系 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-06-15
 */
@Controller
@RequestMapping("/lab-admin-management")
public class LabAdminManagementController {
    @Autowired
    private ILabAdminManagementService labAdminManagementService;
    @Autowired
    private ICollegeService collegeService;
    @Autowired
    private ILaboratoryService laboratoryService;
    @Autowired
    private IUserService userService;

    @GetMapping("/")
    @PreAuthorize("hasRole('admin')")
    public String listLabAdminManagements(Model model) {
        // 获取所有实验室关系，只有用户和实验室关系
        List<LabAdminManagement> list = labAdminManagementService.list();
        model.addAttribute("labAdminManagements", list);
        //获取所有学院表
        List<College> list1 = collegeService.list();
        Map<Integer, String> mapCollege = list1.stream().collect(Collectors.toMap(category -> category.getCollegeId(), category -> category.getCollegeName()));
        model.addAttribute("mapCollege",mapCollege);
        //获取所有实验室表
        List<Laboratory> list2 = laboratoryService.list();
        //实验室与学院id对应
        Map<Integer, Integer> mapLaboratoryCollege = list2.stream().collect(Collectors.toMap(laboratory -> laboratory.getLaboratoryId(), laboratory -> laboratory.getCollegeId()));
        model.addAttribute("mapLaboratoryCollege",mapLaboratoryCollege);
        //实验室与实验室名对应
        Map<Integer, String> mapLaboratory = list2.stream().collect(Collectors.toMap(laboratory -> laboratory.getLaboratoryId(), laboratory -> laboratory.getLaboratoryName()));
        model.addAttribute("mapLaboratory",mapLaboratory);
        //获取所有实验室管理员表
        List<User> list3 = userService.list();
        Map<Integer, String> mapUser = list3.stream().collect(Collectors.toMap(user -> user.getUserId(), user -> user.getUserName()));
        model.addAttribute("mapUser",mapUser);
        return "lab-admin-management-list";
    }


    // 新增页面
    @GetMapping("/add")
    @PreAuthorize("hasRole('admin')")
    public String showAddForm(Model model) {
        model.addAttribute("labmanagement", new LabAdminManagement());
        model.addAttribute("users", userService.list());
        model.addAttribute("laboratories", laboratoryService.list());
        return "lab-admin-management-form";
    }

    // 提交新增
    @PostMapping("/add")
    @PreAuthorize("hasRole('admin')")
    public String addLabAdminManagement(@ModelAttribute LabAdminManagement management) {
        labAdminManagementService.save(management);
        return "redirect:/lab-admin-management/";
    }

    // 编辑页面
    @GetMapping("/edit/{id}")
    @PreAuthorize("hasRole('admin')")
    public String showEditForm(@PathVariable Integer id, Model model) {
        LabAdminManagement management = labAdminManagementService.getById(id);
        model.addAttribute("labmanagement", management);
        model.addAttribute("users", userService.list());
        model.addAttribute("laboratories", laboratoryService.list());
        return "lab-admin-management-form";
    }

    // 提交编辑
    @PostMapping("/update")
    @PreAuthorize("hasRole('admin')")
    public String updateLabAdminManagement(@ModelAttribute LabAdminManagement management) {
        labAdminManagementService.updateById(management);
        return "redirect:/lab-admin-management/";
    }

    // 删除操作
    @GetMapping("/delete/{id}")
    @PreAuthorize("hasRole('admin')")
    public String deleteLabAdminManagement(@PathVariable Integer id) {
        labAdminManagementService.removeById(id);
        return "redirect:/lab-admin-management/";
    }



}
